import React, { Component } from 'react'
import { Table, Space, Button ,message,Popconfirm} from 'antd';
import {connect} from 'react-redux';
import {getShowadminAC} from '../../store/index.js'
import {deleteadmin,getadminId} from '../../apis/admin.js'

class Adminlist extends Component {
     columns = [
        {
          title: '昵称',
          dataIndex: 'adminname',
          key: '_id',
          render: text => <a>{text}</a>,
        },
        {
          title: '创建时间',
          dataIndex: 'time',
          key: 'time',
        },
        {
          title: '用户类型',
          dataIndex: 'userGroup',
          key: 'userGroup',
        },
        {
            title: '状态',
            dataIndex: 'state',
            key: 'state',
          },
        {
          title: '操作',
          key: 'action',
          render: (text, record) => (
            <Space size="middle">
              <Button size="small" type="orange" onClick={()=>this.getadminid(record._id)}>修改</Button>
              <Popconfirm title={`Are you sure delete ${record.adminname}?`}
                        onConfirm={()=>this.confirm(record._id)}
                        onCancel={this.cancel}
                        okText="Yes"
                        cancelText="No">
                        <Button size="middle" type="danger">删除 </Button>
               </Popconfirm>
            </Space>
          ),
        },
      ];
    //
    confirm=async (id)=> {
        // 删除当前选中的数据id
        try {
            console.log("6666666666",id);
            const data =  await deleteadmin(id);
            console.log(data);
            message.success('删除成功');
            this.props.dispatch(getShowadminAC());
        } catch (error) {
            message.success('删除失败');
        }
    }//es5的函数
    
    cancel(e) {
        console.log(e);
        message.error('Click on No');
    }
    getadminid =async (id)=>{
        try {
            console.log("123333333333");
            const data =  await getadminId(id);
            console.log("获取id的data",data);
            console.log(this.props);
            // this.props.history.push("/home/changeadmin");
            this.props.history.push({ pathname: "/home/changeadmin", state: data });
        } catch (error) {
            
        }
    }
    componentDidMount(){
        this.props.dispatch(getShowadminAC());
    }
    onchangepage=(totalNum,pageSize)=>{
      this.props.dispatch(getShowadminAC(totalNum,pageSize));
    }
    render() {
        const {list,totalNum,currentPage,pageSize,loading} =this.props;
        console.log("totalNum",totalNum);
        const pagination ={
          total:totalNum,
          pageSize:4,
          onChange:this.onchangepage,
        }
        return (
            <div>
               <Table columns={this.columns} pagination={pagination}  rowKey="_id" dataSource={list} /> 
            </div>
        )
    }
}

const mapStateToProps =(state)=>{
   const {list,totalNum,currentPage,pageSize,loading} =state;
   return {list,totalNum,currentPage,pageSize,loading};
}

export default connect(mapStateToProps)(Adminlist)
 


 